#include "bc_freeOutflow_waterdepth.hpp"
#include "meshTypes.hpp"
#include "field.hpp"
#include "boundaryPatch.hpp"
#include "unifiedMathFunctions.hpp"

namespace hms
{

#define CHEAD(RETURN_TYPE) \
template<typename MeshType> \
RETURN_TYPE FreeOutflow_waterdepth<MeshType>::

CHEAD() FreeOutflow_waterdepth(
	Field<MeshType>& f,
	const BoundaryPatch& p,
	bool isLimitedArg,
	scalar limitArg
) :
	BaseType( Type::freeOutflow_waterdepth, f, p)
{
	setLimit(isLimitedArg, limitArg);
}

CHEAD(void) update() const {
	if(m_isLimited){
		field().ghostValues( patch() ) =
			max( m_limit, field().domainValues( patch() ) );
	}
	else {
		field().ghostValues( patch() ) = field().domainValues( patch() );
	}
}

CHEAD(bool) isLimited() const {
	return m_isLimited;
}

CHEAD(scalar) limit() const {
	return m_limit;
}

CHEAD(void) isLimited( bool arg ){ m_isLimited = arg; }

CHEAD(void) limit( scalar arg ){
	assert( isLimited() );
	m_limit = arg;
};

CHEAD(void) setLimit( bool isLimitedArg, scalar limitArg ){
	isLimited( isLimitedArg );
	if ( isLimited() )
		limit( limitArg );
}


#undef CHEAD

#define INSTANTIATE_FOR_ALL_MESHTYPES(MeshType) \
template class FreeOutflow_waterdepth<MeshType>;
#include "meshInstantiator.hpp"

}// end namespace hms

